/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client.draw;

import edu.idp.client.interfaces.Drawable;

import java.awt.*;
import java.awt.event.MouseEvent;

/**
 * An extension of Panel which incorporates Drawable objects and
 * the management of the current drawing tools.
 * Handling of mouse events is automatically handled for
 * Drawable objects.<p>
 *
 * @author Kristopher T. Babic
 */
abstract public class AllPurposeDrawPanel extends Panel {

    // the current drawing tool
    private Object current;

    /**
     * gets called when a drawing tool has finished its action
     */
    abstract public void toolEnded(Object ob);

    /**
     * Deactivates the previous drawing tool and activates the specified
     * drawing tool as the current drawing tool.
     *
     * @param ob the new drawing tool to be activated and set as current
     */
    public void setTool(Object ob) {

        // deactivate the previous drawing tool
        if (current != null) {
            if (current instanceof Drawable) {
                Drawable d = (Drawable) current;
                d.setActive(false);
            }
        }

        current = ob;

        // activate the new drawing tool
        if (current != null) {
            if (current instanceof Drawable) {
                Drawable d = (Drawable) current;
                d.setActive(true);
                d.setComponent(this);
            }
        }
    }

    /**
     * returns the current drawing tool
     */
    public Object getTool() {
        return current;
    }

    /**
     * handles all mouse events by the current drawing tool
     *
     * @param event mouse event generated by the current drawing tool
     */
    public void processMouseEvent(MouseEvent event) {
        super.processMouseEvent(event);  // fire to listeners

        // check to see if the drawing tool has completed an action
        if (current != null && event.getID() == MouseEvent.MOUSE_RELEASED)
            toolEnded(current);
    }
}
